import pygame
from helpers import load_image

class Button(pygame.sprite.Sprite):
    '''
    Clickable button using an image
    '''

    callback = None
    is_visible = True   # Only draw button if true
    
    def __init__(self, image_name, position, callback):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image( image_name,-1)
        self.rect.topleft = position
        self.callback = callback
    
    def on_click(self, *args):
        '''
        Called when the button is clicked, passing any arguments given
        to the callback function.
        '''
        self.callback(*args)
    
    def draw(self, view_port):
        '''
        Draws the button on the screen.
        '''
        if self.is_visible:
            view_port.screen.blit(self.image, self.rect)